package com.framework.bdf4j.comm.download;

import com.framework.bdf4j.comm.util.FileUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;


/**
 * @describle 该类用来处理response对象，采用了简单单例来写(没有考虑多线程环境)
 */
public class DownLoadResponseSetting implements HttpServletResponseSetting {

    private static DownLoadResponseSetting downLoadResponseSetting = null;

    private DownLoadResponseSetting () {

    }

    @Override
    public HttpServletResponse convertServlet(HttpServletRequest request,HttpServletResponse response, String fileName) throws UnsupportedEncodingException {

        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition",
                "attachment;fileName=" + FileUtils.setFileDownloadHeader(request,fileName));
        return response;
    }

    public static DownLoadResponseSetting getInstance() {
        if(downLoadResponseSetting == null) {
            downLoadResponseSetting = new DownLoadResponseSetting();
        }
        return downLoadResponseSetting;
    }
}
